<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="员工姓名" prop="nickName">
        <el-select
          filterable
          v-model="queryParams.nickName"
          placeholder="请选择员工姓名"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in userNameList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.nickName"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="日期">
        <el-date-picker
          v-model="queryParams.dtRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="最早完工日期"
          end-placeholder="最晚完工日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:workingHours:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"   :columns="columns"></right-toolbar>
    </el-row>

    <!-- 合计：   show-summary :summary-method="getTotal"-->
    <el-table v-loading="loading" stripe :data="workingHoursList" :highlight-current-row="true">
      <el-table-column label="序号" align="center" width="80" v-if="columns[0].visible">
        <template slot-scope="scope">
          <div>
            {{scope.$index+1}}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="员工姓名" align="center" prop="nickName" width="300" v-if="columns[1].visible"/>

      <el-table-column label="定额工时(小时)" align="center" prop="quotaWorkingHours" width="300" v-if="columns[2].visible">
        <template slot-scope="scope">
          <span> {{ Math.floor((scope.row.quotaWorkingHours)) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实作工时(小时)" align="center" prop="workingHours" width="300" v-if="columns[3].visible">
        <template slot-scope="scope">
          <span> {{ Math.floor((scope.row.workingHours)) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="最早完工日期" align="center" prop="dtDoneStart" v-if="columns[4].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dtDoneStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="最晚完工日期" align="center" prop="dtDoneEnd" v-if="columns[5].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dtDoneEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>

</template>

<script>
import { listWorkingHours } from "@/api/mes/workingHours";
import { listUser} from "@/api/system/user";
export default {
  name: "WorkingHours",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工时管理表格数据
      workingHoursList: [],
      userNameList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核意见时间范围
      daterangeDocDt: [],
      // 审核意见时间范围
      daterangeConfirmDt: [],
      // 审核意见时间范围
      daterangeCreateTime: [],
      // 审核意见时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fkWorkerUserId: null,
        fkMesDispatchUuid: null,
        mesDictQuotaLevel: null,
        nickName:null,
        hours: null,
        sysNormalDisable: null,
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: null,
        confirmNote: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fkWorkerUserId: [
          { required: true, message: "工作人员不能为空", trigger: "blur" }
        ],
        fkMesDispatchUuid: [
          { required: true, message: "生产派工表ID不能为空", trigger: "blur" }
        ],
        hours: [
          { required: true, message: "工时不能为空", trigger: "blur" }
        ],
        sysNormalDisable: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        docNum: [
          { required: true, message: "单据编号不能为空", trigger: "blur" }
        ],
        docDt: [
          { required: true, message: "单据时间不能为空", trigger: "blur" }
        ],
        fkAttentionUserId: [
          { required: true, message: "经办人不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `员工姓名`, visible: true },
        { key: 2, label: `定额工时(小时)`, visible: true },
        { key: 3, label: `实作工时(小时)`, visible: true },
        { key: 4, label: `最早完工日期`, visible: true },
        { key: 5, label: `最晚完工日期`, visible: true },
      ],
    };
  },
  created() {
    this.getList_userName();
    this.getList();
    // this.getTotal();
  },

  methods: {
    // getTotal(param) {
    //   const { columns, data } = param;
    //   const sums = [];
    //   columns.forEach((column, index) => {
    //     if (index === 0) {
    //       sums[index] = '合计';
    //       return;
    //     }
    //     const values = data.map(item => Number(item[column.property]));
    //     if (column.property === 'workingHours' || column.property === 'quotaWorkingHours') {
    //       sums[index] = values.reduce((prev, curr) => {
    //         const value = Number(curr);
    //         if (!isNaN(value)) {
    //           return prev + curr;
    //         } else {
    //           return prev;
    //         }
    //       }, 0);
    //     } else {
    //       sums[index] = '--';
    //     }
    //   });
    //
    //   return sums;
    // },

    getList_userName(){
      listUser().then(response => {
        this.userNameList = response.rows
      });
    },
    /** 查询工时管理列表 */
    getList() {
      this.loading = true;
      if( this.queryParams.dtRange ){
        this.queryParams.dtDoneStart = this.queryParams.dtRange[0]
        this.queryParams.dtDoneEnd = this.queryParams.dtRange[1]
      }else {
        this.queryParams.dtDoneStart = null
        this.queryParams.dtDoneEnd = null
      }
      listWorkingHours(this.queryParams).then(response => {
        this.workingHoursList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workingHoursUuid: null,
        fkWorkerUserId: null,
        fkMesDispatchUuid: null,
        mesDictQuotaLevel: null,
        hours: null,
        sysNormalDisable: "0",
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: "0",
        confirmNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/workingHours/export', {
        ...this.queryParams
      }, `mes_workingHours.xlsx`)
    }
  }
};
</script>
